import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:matisse_android_plugin/src/ios/screen_util.dart';

enum ClickType {
  camera,
  gallery,
  video,
}

class ChooseMatisseTypeUtil {
  ///选择类型弹窗
  static Future<ClickType> showChooseTypeDialog(BuildContext context,
      {Function onResult, int maxImageSize, int maxVideoSize}) async {
    List itemList;

//    HomePostItemVo cameraItemVo = HomePostItemVo(
//        iconUrl: "images/paizhao.png",
//        text: "拍照",
//        navFunction: () {
//          return Navigator.of(context).pop(ClickType.camera);
//        });
    HomePostItemVo galleryItemVo = HomePostItemVo(
        iconUrl: "images/xiangce.png",
        text: "图库",
        navFunction: () {
          return Navigator.of(context).pop(ClickType.gallery);
        });
    HomePostItemVo videoItemVo = HomePostItemVo(
        iconUrl: "images/paishe.png",
        text: "视频",
        navFunction: () {
          return Navigator.of(context).pop(ClickType.video);
        });

    if (maxImageSize != null &&
        maxImageSize > 0 &&
        maxVideoSize != null &&
        maxVideoSize > 0) {
      itemList = [galleryItemVo, videoItemVo];
    } else if (maxImageSize != null && maxImageSize > 0) {
      if (galleryItemVo != null) {
        videoItemVo?.navFunction();
      }
    } else if (maxVideoSize != null && maxVideoSize > 0) {
      if (videoItemVo != null) {
        videoItemVo?.navFunction();
      }
    }

    return await showModalBottomSheet<ClickType>(
        context: context,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) {
          return Container(
            height: 64 + 40 + ScreenMatisseUtil.getBottomBarH(context),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.vertical(top: Radius.circular(14)),
                color: Colors.white),
            child: Column(
              children: <Widget>[
                Container(
                  height: 20,
                ),
                Container(
                  width: ScreenMatisseUtil.getScreenW(context),
                  height: 64,
                  child: Row(
                    children: itemList.map((item) {
                      return Expanded(
                          child: GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTap: () => item?.navFunction(),
                        child: HomePostItemWidget(
                          item: item,
                        ),
                      ));
                    }).toList(),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      Navigator.of(context).pop();
                      return null;
                    },
                    child: Container(),
                  ),
                ),
              ],
            ),
          );
        });
  }
}

///首页发布单项
class HomePostItemWidget extends StatelessWidget {
  final HomePostItemVo item;

  const HomePostItemWidget({Key key, @required this.item})
      : assert(item != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      constraints: BoxConstraints.expand(),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Image.asset(
            item?.iconUrl ?? "",
            width: 34,
            height: 34,
            package: "matisse_android_plugin",
          ),
          Spacer(),
          Text(
            item?.text ?? "",
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(color: Color(0xFF222222), fontSize: 14),
          )
        ],
      ),
    );
  }
}

///首页发布单项值
class HomePostItemVo {
  String iconUrl;
  String text;
  Function navFunction;

  HomePostItemVo({this.iconUrl, this.text, this.navFunction});
}
